//-----------------------------------------------------------------------
// <copyright file="IDSSortDAO.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
	
using System;
using System.Data;
using MIMS.WRS.Entities;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace MIMS.WRS.Data
{	
	public class ShoppingCustomerDAO
	{
		public ShoppingCustomerDAO()
		{
		}

		#region Common methods
		public virtual ShoppingCustomer CreateShoppingCustomerFromReader(IDataReader reader)
		{
			ShoppingCustomer item = new ShoppingCustomer();
			try
			{
				if (!reader.IsDBNull(reader.GetOrdinal("CustomerID"))) item.CustomerID = (int)reader["CustomerID"];
				if (!reader.IsDBNull(reader.GetOrdinal("CompanyName"))) item.CompanyName = (string)reader["CompanyName"];
				if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
				if (!reader.IsDBNull(reader.GetOrdinal("FamilyName"))) item.FamilyName = (string)reader["FamilyName"];
				if (!reader.IsDBNull(reader.GetOrdinal("GivenName"))) item.GivenName = (string)reader["GivenName"];
				if (!reader.IsDBNull(reader.GetOrdinal("StreetAddress"))) item.StreetAddress = (string)reader["StreetAddress"];
				if (!reader.IsDBNull(reader.GetOrdinal("City"))) item.City = (string)reader["City"];
				if (!reader.IsDBNull(reader.GetOrdinal("State"))) item.State = (string)reader["State"];
				if (!reader.IsDBNull(reader.GetOrdinal("ZipCode"))) item.ZipCode = (string)reader["ZipCode"];
				if (!reader.IsDBNull(reader.GetOrdinal("Country"))) item.Country = (string)reader["Country"];
				if (!reader.IsDBNull(reader.GetOrdinal("EmailAddress"))) item.EmailAddress = (string)reader["EmailAddress"];
				if (!reader.IsDBNull(reader.GetOrdinal("PhoneNumber"))) item.PhoneNumber = (string)reader["PhoneNumber"];
				
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessCreateShoppingCustomerFromReaderException, ex);
			}
			return item;
		}
		#endregion
        
		#region CreateShoppingCustomer methods
            
		public virtual void CreateShoppingCustomer(ShoppingCustomer shoppingCustomer)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShoppingCustomerInsert");
                
				dbCommandWrapper.AddInParameter("@CompanyName", DbType.String, shoppingCustomer.CompanyName);
				dbCommandWrapper.AddInParameter("@Title", DbType.String, shoppingCustomer.Title);
				dbCommandWrapper.AddInParameter("@FamilyName", DbType.String, shoppingCustomer.FamilyName);
				dbCommandWrapper.AddInParameter("@GivenName", DbType.String, shoppingCustomer.GivenName);
				dbCommandWrapper.AddInParameter("@StreetAddress", DbType.String, shoppingCustomer.StreetAddress);
				dbCommandWrapper.AddInParameter("@City", DbType.String, shoppingCustomer.City);
				dbCommandWrapper.AddInParameter("@State", DbType.String, shoppingCustomer.State);
				dbCommandWrapper.AddInParameter("@ZipCode", DbType.String, shoppingCustomer.ZipCode);
				dbCommandWrapper.AddInParameter("@Country", DbType.String, shoppingCustomer.Country);
				dbCommandWrapper.AddInParameter("@EmailAddress", DbType.String, shoppingCustomer.EmailAddress);
				dbCommandWrapper.AddInParameter("@PhoneNumber", DbType.String, shoppingCustomer.PhoneNumber);
				dbCommandWrapper.AddOutParameter("@CustomerID", DbType.Int32, 0);
                
				DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
				shoppingCustomer.CustomerID = (int)dbCommandWrapper.GetParameterValue("@CustomerID");
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessCreateShoppingCustomerException, ex);
			}
		}

		#endregion

		#region UpdateShoppingCustomer methods
        
		public virtual void UpdateShoppingCustomer(ShoppingCustomer shoppingCustomer)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShoppingCustomerUpdate");            
                
				dbCommandWrapper.AddInParameter("@CustomerID", DbType.Int32, shoppingCustomer.CustomerID);
				dbCommandWrapper.AddInParameter("@CompanyName", DbType.String, shoppingCustomer.CompanyName);
				dbCommandWrapper.AddInParameter("@Title", DbType.String, shoppingCustomer.Title);
				dbCommandWrapper.AddInParameter("@FamilyName", DbType.String, shoppingCustomer.FamilyName);
				dbCommandWrapper.AddInParameter("@GivenName", DbType.String, shoppingCustomer.GivenName);
				dbCommandWrapper.AddInParameter("@StreetAddress", DbType.String, shoppingCustomer.StreetAddress);
				dbCommandWrapper.AddInParameter("@City", DbType.String, shoppingCustomer.City);
				dbCommandWrapper.AddInParameter("@State", DbType.String, shoppingCustomer.State);
				dbCommandWrapper.AddInParameter("@ZipCode", DbType.String, shoppingCustomer.ZipCode);
				dbCommandWrapper.AddInParameter("@ZipCode", DbType.String, shoppingCustomer.ZipCode);
				dbCommandWrapper.AddInParameter("@EmailAddress", DbType.String, shoppingCustomer.EmailAddress);
				dbCommandWrapper.AddInParameter("@PhoneNumber", DbType.String, shoppingCustomer.PhoneNumber);
                
				DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("DataAccessUpdateShoppingCustomerException", ex);
			}
		}
        
		#endregion

		#region DeleteShoppingCustomer methods
		public virtual void DeleteShoppingCustomer(int customerID)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShoppingCustomerDelete");
                
				dbCommandWrapper.AddInParameter("@CustomerID", DbType.Int32, customerID);
                
				DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("DataAccessDeleteShoppingCustomerException", ex);
			}
		}

		#endregion

		#region GetShoppingCustomer methods
        
		public virtual ShoppingCustomer GetShoppingCustomer(int shoppingOrderId)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShoppingCustomerGet");
                
				dbCommandWrapper.AddInParameter("@ShoppingOrderId", DbType.Int32, shoppingOrderId);
                
				ShoppingCustomer shoppingCustomer =  null;
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					if (reader.Read())
					{
						shoppingCustomer = CreateShoppingCustomerFromReader(reader);
						reader.Close();
					}
				}
				return shoppingCustomer;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("DataAccessGetShoppingOrderException", ex);
			}
		}

		#endregion

		public virtual ShoppingCustomerOrder CreateShoppingCustomerOrderFromReader(IDataReader reader)
		{
			ShoppingCustomerOrder item = new ShoppingCustomerOrder();
			try
			{
				if (!reader.IsDBNull(reader.GetOrdinal("ShoppingOrderId"))) item.ShoppingOrderId = (int)reader["ShoppingOrderId"];
				if (!reader.IsDBNull(reader.GetOrdinal("Id"))) item.UserId = (int)reader["Id"];
				if (!reader.IsDBNull(reader.GetOrdinal("CustomerID"))) item.CustomerID = (int)reader["CustomerID"];
				if (!reader.IsDBNull(reader.GetOrdinal("OrderDate"))) item.OrderDate = (DateTime)reader["OrderDate"];
				if (!reader.IsDBNull(reader.GetOrdinal("CompanyName"))) item.CompanyName = (string)reader["CompanyName"];
				if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
				if (!reader.IsDBNull(reader.GetOrdinal("FamilyName"))) item.FamilyName = (string)reader["FamilyName"];
				if (!reader.IsDBNull(reader.GetOrdinal("GivenName"))) item.GivenName = (string)reader["GivenName"];
				if (!reader.IsDBNull(reader.GetOrdinal("StreetAddress"))) item.StreetAddress = (string)reader["StreetAddress"];
				if (!reader.IsDBNull(reader.GetOrdinal("City"))) item.City = (string)reader["City"];
				if (!reader.IsDBNull(reader.GetOrdinal("State"))) item.State = (string)reader["State"];
				if (!reader.IsDBNull(reader.GetOrdinal("ZipCode"))) item.ZipCode = (string)reader["ZipCode"];
				if (!reader.IsDBNull(reader.GetOrdinal("Country"))) item.Country = (string)reader["Country"];
				if (!reader.IsDBNull(reader.GetOrdinal("EmailAddress"))) item.EmailAddress = (string)reader["EmailAddress"];
				if (!reader.IsDBNull(reader.GetOrdinal("UserId"))) item.UserId = (int)reader["UserId"];
				if (!reader.IsDBNull(reader.GetOrdinal("Qty20"))) item.Qty20 = (int)reader["Qty20"];
				if (!reader.IsDBNull(reader.GetOrdinal("Qty40"))) item.Qty40 = (int)reader["Qty40"];
				if (!reader.IsDBNull(reader.GetOrdinal("Qty40HC"))) item.Qty40HC = (int)reader["Qty40HC"];
				if (!reader.IsDBNull(reader.GetOrdinal("OrderType"))) item.OrderType = (bool)reader["OrderType"];
				if (!reader.IsDBNull(reader.GetOrdinal("OrderNo"))) item.OrderNo = (string)reader["OrderNo"];
				if (!reader.IsDBNull(reader.GetOrdinal("HalfPalletSpaces"))) item.HalfPalletSpaces = (int)reader["HalfPalletSpaces"];
				if (!reader.IsDBNull(reader.GetOrdinal("FilledVolume"))) item.FilledVolume = (decimal)reader["FilledVolume"];
				if (!reader.IsDBNull(reader.GetOrdinal("DestinationCountryId"))) item.DestinationCountryId = (int)reader["DestinationCountryId"];
				if (!reader.IsDBNull(reader.GetOrdinal("ContPointDescription"))) item.ContPointDescription = (string)reader["ContPointDescription"];
				if (!reader.IsDBNull(reader.GetOrdinal("PhoneNumber"))) item.PhoneNumber = (string)reader["PhoneNumber"];
				
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("DataAccessCreateShoppingCustomerOrderFromReaderException", ex);
			}
			return item;
		}
		public virtual ShoppingCustomerOrderCollection GetShoppingCustomerOrderCollection()
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShoppingCustomerOrderGetAll");
                
				ShoppingCustomerOrderCollection shoppingList = new ShoppingCustomerOrderCollection();
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					while (reader.Read())
					{
						ShoppingCustomerOrder shoppingItem = CreateShoppingCustomerOrderFromReader(reader);
						shoppingList.Add(shoppingItem);
					}
					reader.Close();
				}
				return shoppingList;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("DataAccessGetShoppingCustomerOrderCollectionException", ex);
			}
		}

		public virtual IDataReader GetShoppingCustomerForReport()
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShoppingCustomerOrderGetAll");
                
				ShoppingCustomerOrderCollection shoppingList = new ShoppingCustomerOrderCollection();
				return  DataAccessHelper.ExecuteReader(database, dbCommandWrapper);
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("DataAccessGetShoppingCustomerForReportException", ex);
			}
		}
		public virtual IDataReader GetShoppingCustomerForReportByShoppingOrderId(int shoppingOrderId)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spShoppingCustomerOrderGetAllByShoppingOrderId");
                dbCommandWrapper.AddInParameter("@ShoppingOrderId", DbType.Int32, shoppingOrderId);

				return  DataAccessHelper.ExecuteReader(database, dbCommandWrapper);
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("DataAccessGetShoppingCustomerForReportException", ex);
			}
		}
	}
}
